<?php
 
$form = $this->element;

foreach ($form->getElements() as $e) {
	if ($e instanceof Zend_Form_Element_Checkbox) {
		$label = $e->getDecorator('Label');
		$label->setOption('placement', Zend_Form_Decorator_Abstract::APPEND);
		$label->setTag('dd');
		
		$tag = $e->getDecorator('HtmlTag');
		$tag->setTag('dt');
	}
}
$userListCount = 0;

?>

<form enctype="multipart/form-data" method="post" action="">
	<div>
	
  <?php 
    /**
     * Translate some form errrors.
     * 
     * @param string $key
     * @param string $error
     * @return string
     */
    function _translate($key, $error)
    {
    	$_translated = array(
			'isEmpty' => 'Поле обязательно для заполнения',
		);
	
    	if (isset($_translated[$key])) {
    		return $_translated[$key];
    	}
    	
    	return $error;
    }
    
    $errors = $form->getMessages();
    foreach ($errors as $key => $error) {
    	if (is_array($error)) {
    		unset($errors[$key]);
    	}

   		if (is_array($error)) {
     		foreach ($error as $k => $v) {
     			$label = $form->getElement($key)->getLabel();
     			$v = _translate($k, $v);
     			$errors[$key][$k] = "$label -- $v";
     		}
    	} else {
    		$errors[$key] = _translate($key, $error);
    	}
    }
    
    if ($errors) {
	    echo "<ul class='errors'>";
	    foreach ($errors as $error) {
	    	if (is_array($error)) {
	    		foreach ($error as $k => $v) {
	    			echo "<li>$v</li>";
	    		}
	    	} else {
	    		echo "<li>$error</li>";
	    	}
	    }
	    echo "</ul>";
    }
    ?>
	
		<?php if ($form->getRow()) :?>
			<fieldset style="float:right;width:400px">
				<legend>Версии страницы</legend>
				<br/>
				<?php 
					$pageRow = $form->getRow()->getRow();
					$rowset = PagesService::getInstance()->getLastPageVersion($pageRow, 10);
					$domain = Config::getInstance()->hostname;
					$preHref = $domain.'/pages/'.$pageRow->getKey().'/ver/';
					/* @var $versionRow StaticPageVersionRow */
					foreach ($rowset as $versionRow) {
						echo '<dl class="floater-3"> <a href="'.$preHref.$versionRow->getId().'/">'.$versionRow->getCreatedAtVersion().'</a></dl>';
					}
				
				?>
				<br/>
			</fieldset>
		<?php endif; ?>
		
		<dl class="floater-3">
			<?php echo $form->getElement('additional_text'); ?>
	   </dl>
	   
	   	<dl class="floater-3"> 
			<?php echo $form->getElement('content'); ?>
	   </dl>
	
		<dl class="floater-3">  
	      <?php echo $form->getElement('section_id'); ?>
	    </dl>
	
		<dl class="floater-3">
			<?php echo $form->getElement('description'); ?>
	   	</dl>
	   	
	   	<dl class="floater-3">
			<?php echo $form->getElement('keywords'); ?>
	    </dl>
	    
	    <dl class="floater-3">
			<?php echo $form->getElement('meta_title'); ?>
	    </dl>
	
		<dl class="floater-3">
			<?php echo $form->getElement('not_search'); ?>
	    </dl>
	    
	    <dl class="floater-3">
			<?php echo $form->getElement('title'); ?>
	    </dl>
	    
	    <dl class="floater-3">
			<?php echo $form->getElement('key'); ?>
	    </dl>
	    
	    <dl class="floater-3">
			<?php echo $form->getElement('url'); ?>
	    </dl>
	    
	    <dl class="floater-3">
			<?php echo $form->getElement('ord'); ?>
	    </dl>
	
		<?php if ($form->getRow()) :?>
			<dl>
			<fieldset style="float:right;width:400px">
				<legend>Пользователи</legend>
				<br/>
				<?php 
					$pageRow = $form->getRow()->getRow();
					$userList = PagesService::getInstance()->getApproverUserList($pageRow);
					$userListCount = $userList->count();
					$pageVersion = PagesService::getInstance()->getLastPageVersion($pageRow);
					$approvedRowset = array();
					if ($pageVersion && !$pageVersion->getIsApprovedVersion()) {
						$approvedRowset = PagesService::getInstance()->getAllApprovedByAdminUser($pageVersion);
						$approvedRowset = StaticPageVersionApprovePeer::getInstance()->normalizeByPreferKey($approvedRowset, 'admin_user_id');
					}
					$checkedCount = 0;
					if ($userList) {
						echo '<div>';
						/* @var $adminUser AdminUserRow */
						foreach ($userList as $adminUser) {
							$checked = '';
							if (isset($approvedRowset[$adminUser->getId()])) {
								++$checkedCount;
								$checked = 'checked="checked"';
							}
							echo '<div><label><input disabled="disabled" type="checkbox" '.$checked.'>'.$adminUser->getName().'</label></div>';
						}
						echo '</div>';
					}
				?>
				<br/>
			</fieldset>
			</dl>
		<?php endif; ?>
	    	
	
	    <dl class="floater-3">
			<?php echo $form->getElement('show_menu'); ?>
	    </dl>
	    
	    <dl class="floater-3">
			<?php echo $form->getElement('in_nav'); ?>
	    </dl>
	    
	    <dl class="floater-3">
			<?php echo $form->getElement('is_pub'); ?>
	    </dl>
	    
	    <dl class="floater-3">
			<?php echo $form->getElement('admin_user_is_approved_version'); ?>
	    </dl>
		<dt id="submit-label">&nbsp;</dt>
		<dd id="submit-element">
			<?php /* @var $elem Zend_Form_Element */  $elem = $form->getElement('key'); ?>
			<?php if ($elem->getAttrib('readonly')) : ?>
				<input type="submit" name="leave_page" value="Выйти" style="width:300px; height:50px" />
			<?php else: ?>
				<input type="submit" name="submit" id="submit" value="Сохранить" />
					<?php echo $form->getElement('crud_exit'); ?>
			<?php endif;?>
			<?php if ($form->getRow()) :?>
				<?php if ($userListCount == 0):?>
					<input type="submit" name="save_approved_page"  value="сохранить и утвердить" style="width:300px; height:50px" />
				<?php elseif ($userListCount <= $checkedCount) :?>
					<input type="submit" name="approved_page" value="Утвердить" style="width:300px; height:50px" />
					все необходимые подтвержения собраны, можно и нужно нажать "Утвердить", чтобы опубликовать страницу
				<?php endif;?>
			<?php endif;?>
		</dd>
	</div>
</form>